﻿using System;
using System.Collections.Generic;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using OnlineHelpDesk.BLL_Service;

namespace OnlineHelpDesk
{
    public partial class LoginOHD : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                txtname.Text = "";
                txtpw.Text = "";
                txtname.Focus();
            }
        }

        protected void btnReset_Click(object sender, EventArgs e)
        {
            txtname.Text = "";
            txtpw.Text = "";
            txtname.Focus();
        }

        private void CheckLogin(string UserName, string Password)
        {
            Service1 s = new Service1();
            int numCheck = 0;
            numCheck = s.CheckLogin(UserName, Password);
            switch (numCheck)
            {
                case 0:
                    Session["UserName"] = txtname.Text.ToString().ToLower();
                    Session["Password"] = txtpw.Text.ToString();
                    break;
                case 1:
                    lblMessage.Visible = true;
                    lblMessage.Text = "Invalid user name, please try again!!";
                    break;
                case 2:
                    lblMessage.Visible = true;
                    lblMessage.Text = "Invalid password!!";
                    break;
            }
        }

        protected void btnLogin_Click(object sender, EventArgs e)
        {
            Service1 s = new Service1();
            CheckLogin(txtname.Text + "", txtpw.Text + "");
            if (Session["UserName"] != null || Session["Password"] != null)
            {
                if (s.getNameLogin(txtname.Text) == 1)
                {
                    Response.Redirect("Admin/AdminHome.aspx?Admin=" + txtname.Text);
                }
                else
                    if (s.getNameLogin(txtname.Text) == 2)
                    {
                        Response.Redirect("Assignee/AssigneeHome.aspx?Assignee=" + txtname.Text);
                    }
                    else
                        if (s.getNameLogin(txtname.Text) == 3)
                        {
                            Response.Redirect("FacilityHead/FacilityHeadHome.aspx?FacilityHead=" + txtname.Text);
                        }
                        else
                            if (s.getNameLogin(txtname.Text) == 4)
                            {
                                Response.Redirect("Requestor/RequestorHome.aspx?Requestor=" + txtname.Text);
                            }
            }
            else
            {
                lblMessage.Visible = true;
            }
        }
    }
}